using AbpMvc.Demo.Products;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
using System.Linq;
using System.Threading.Tasks;

namespace AbpMvc.Demo.Web.Pages.Products
{
    public class CreateProductModalModel : DemoPageModel
    {
        public SelectListItem[] Categories { get; set; }

        [BindProperty]
        public CreateEditProductViewModel Product { get; set; }

        private readonly IProductService productService;

        public CreateProductModalModel(IProductService productService)
        {
            this.productService = productService;
        }

        public async Task OnGetAsync()
        {
            Product = new CreateEditProductViewModel
            {
                ReleaseDate = Clock.Now,
                StockState = ProductStockState.PreOrder
            };
            var categoryLookup = await  productService.GetCategoriesAsync();
            Categories = categoryLookup.Items.Select(p => new SelectListItem(p.Name
                , p.Id.ToString())).ToArray();

        }
        public async Task<IActionResult> OnPostAsync()
        {
            var productDto = ObjectMapper.Map<CreateEditProductViewModel, CreateOrUpdateProductDto>(Product);
            await productService.CreateAsync(productDto);
            return NoContent();
        }
    }
}
